/*
* @Author: Someu
* @Date:   2017-12-03 21:47:06
* @Last Modified by:   Someu
* @Last Modified time: 2017-12-04 10:19:55
*/
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
require('page/common/nav-side/index.js');
require('page/common/index.js');
require('page/index/index.css');

var templateIndex   = require('./index.string');
var _user           = require('service/user-service.js');
var _mm             = require('util/mm.js');
var navSide         = require('page/common/nav-side/index.js');

var page = {    
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    bindEvent : function(){
        var _this = this;
        //点击保存按钮后动作
        $('.btn.btn-submit').click(function(){
            var userInfo = {
                phone       : $.trim($('#phone').val()),
                email       : $.trim($('#email').val()),
                question    : $.trim($('#question').val()),
                answer      : $.trim($('#answer').val())
            };
            validateResult = _this.validateForm(userInfo);
            if(validateResult.status){
                _user.updateUserInfo(userInfo,function(res){
                    _mm.successTips(res);
                    window.location.href = './user-center.html';
                },function(errMsg){
                    _mm.errorTips(errMsg);
                })
            }
            else{
                _mm.errorTips(validateResult.msg);
            }
        });
    },
    validateForm : function(formData){
        var result = {
            status : false,
            msg : ''
        };
        //验证手机号
         if(!_mm.validata(formData.phone, 'phone')){
            result.msg = '手机号格式不正确';
            return result;
        }
        //验证邮箱是否合理
        if(!_mm.validata(formData.email, 'email')){
            result.msg = '邮箱格式不正确';
            return result;
        }
        //验证密码提示问题是否为空
        if(!_mm.validata(formData.question, 'require')){
            result.msg = '密码提示问题不能为空';
            return result;
        }
        //验证密码提示问题是否为空
        if(!_mm.validata(formData.answer, 'require')){
            result.msg = '密码提示问题答案不能为空';
            return result;
        }
        //验证通过
        result.status   = true;
        result.msg      = '验证通过';
        return result;
    },
    onLoad : function(){
        navSide.init({
            name : 'user-center'
        });
        this.loadUserInfo();
    },
    loadUserInfo : function(){
        $('.panel-body').html('<div class="loading"></div>');
        var userHtml = '';
        /*实际代码*/
        /*_user.getUserInfo(function(res){
            userHtml = _mm.renderHtml(templateIndex,res);
            $('.panel-body').html(userHtml);
        },function(errMsg){
            _mm.errorTips(errMsg);
        }) */
        /*实际代码结束*/
        /*测试代码*/
        var resTest = {
            user    : '张三',
            phone   : '13112341234',
            email   : 'zhangsan@test.com',
            question: '我？',
            answer  : '张三'
        };
        userHtml = _mm.renderHtml(templateIndex,resTest);
        $('.panel-body').html(userHtml);
        /*测试代码结束*/
    }
};

$(function(){
    page.init();
});